﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Framework.Web;
using HF.WorkFlow.Template;
using HF.WorkFlow.Engine;
using HF.BaseWebCtrlLib;
using HF.Framework.DataClientDBAgent;
using HF.Framework.SqlDataContract;

public partial class BasePages_UnClaimTask : System.Web.UI.Page
{
    string UserId = "";
    DataTable mainDt;
    protected void Page_Load(object sender, EventArgs e)
    {
        //BaseUserPage.LoadWaitPage();//加上就出错，不能自动关闭不知道啥原因。
        UserId = this.User.Identity.Name;
        if (!this.IsPostBack)
        {
            InitData();
        }
       
    }
    public void BuildNoRecords(GridView gridView, DataTable dt)
    {
        try
        {
            if (dt.Rows.Count == 0)
            {
                dt.Rows.Add(dt.NewRow());
                gridView.DataSource = dt;
                gridView.DataBind();
                int columnCount = gridView.Rows[0].Cells.Count;
                gridView.Rows[0].Cells.Clear();
                gridView.Rows[0].Cells.Add(new TableCell());
                gridView.Rows[0].Cells[0].ColumnSpan = columnCount;
                gridView.Rows[0].Cells[0].Text = "没有数据.";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void InitData()
    {
        mainDt = WorkFlowInstance.GetInstance().GetClaimWorkTasks(UserId,1000);
        gvClaimingTask.DataSource = mainDt;
        gvClaimingTask.DataBind();
        BuildNoRecords(gvClaimingTask,mainDt);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
       
    }
    protected void btnWorkflowMap_Click(object sender, EventArgs e)
    {
        
        
    }
    protected void gvClaimingTask_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string[]   parms   =   (sender as LinkButton).CommandArgument.Split( ',');
        WorkFlowRuntime wfruntime = new WorkFlowRuntime();
        string OperatorInstanceId = parms[1];
        wfruntime.TaskClaim(UserId,OperatorInstanceId);
        Response.Redirect(Request.RawUrl);//刷新页面
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string filedstr = "Priority,WorkFlowNo,taskStartTime,TaskInstanceCaption,FlowInstanceCaption,OperContent,Status,FlowCaption," +
                         "TaskCaption,UserId,WorkFlowId,WorkTaskId,WorkFlowInstanceId,WorkTaskInstanceId,OperType,TaskTypeId,operatorInstanceId," +
                          "OperatedDes,OperDateTime,taskEndTime,flowStartTime,flowEndTime,pOperatedDes,Description,OperStatus,taskInsType,TaskInsDescription";
        string sqlstr = "";
        DataTable dt;
        if (cbxWorkflowCaption.Checked==false  && cbxDatetime.Checked == false)
        {
            WorkDialog.ShowMessage(this,"请先选择查询条件!");
            return;
        }
        SqlDataItem sqlItem = new SqlDataItem();
        sqlstr = "select * from (";
        sqlstr =sqlstr+ "  select  "+filedstr+" from HF_WorkTaskInstanceView  WHERE ";
        sqlstr = sqlstr + " ((OperContent IN (SELECT OperContent FROM HF_OperContentView where UserId='" + UserId + "') ) OR ";
        sqlstr = sqlstr + " (OperContent = 'ALL')) and  (OperStatus='0') and   ";
        sqlstr = sqlstr + " (Status='1' or Status='5')and (flStatus <>'4') and (flStatus <>'5')  ";//不是终止的 ，不是挂起的
        sqlstr = sqlstr + " union ";
        sqlstr = sqlstr + " select " + filedstr + " from HF_AccreditInstanceView where ";
        sqlstr = sqlstr + " AccreditToUserId='" + UserId + "' and AccreditStatus='1'and  (status='1' or status='5' ) and flStatus <>'4' and flStatus <>'5'";//不是终止的 ，不是挂起的
        sqlstr = sqlstr + ") a  where 1=1 and ";
        if (cbxWorkflowCaption.Checked == true)
        {
            sqlstr = sqlstr + " FlowInstanceCaption like @FlowInsCaption and ";
            sqlItem.AppendParameter("@FlowInsCaption", "%" + tbxFlowInsCaption.Text + "%");
        }
        else
        {
            sqlstr = sqlstr + " 1 =@FlowInsCaption and ";
            sqlItem.AppendParameter("@FlowInsCaption", "1");
        }
        if (cbxDatetime.Checked == true)
        {
            sqlstr = sqlstr + " taskStartTime >= @taskStartTime and ";
            sqlstr = sqlstr + " taskStartTime <= @taskEndTime  ";
            sqlItem.AppendParameter("@taskStartTime", YYSimpleCalendar1.Text);
            sqlItem.AppendParameter("@taskEndTime", YYSimpleCalendar2.Text);
        }
        else
        {
            sqlstr = sqlstr + " 1 = @taskStartTime and ";
            sqlstr = sqlstr + " 1 = @taskEndTime  ";
            sqlItem.AppendParameter("@taskStartTime", "1");
            sqlItem.AppendParameter("@taskEndTime", "1");
        }
        sqlstr = sqlstr + " order by Priority, taskStartTime desc ";
        sqlItem.CommandText = sqlstr;
        ClientDBAgent agent = new ClientDBAgent();
        dt = agent.ExecuteDataTable(sqlItem);
        gvClaimingTask.DataSource = dt;
        gvClaimingTask.DataBind();
        BuildNoRecords(gvClaimingTask, dt);
        
    }
    protected void gvClaimingTask_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        BaseUserPage.TaskGvPriorityBound(e, 6);
    }
}
